home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mint96sb.zoo / src / signal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-18  |  15.8 KB  |  598 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith. All rights reserved.
  3. */
  4.  
  5. /* signal.c:: signal handling routines */
  6.  
  7. #include "mint.h"
  8.  
  9. void (*sig_routine)();    /* used in intr.s */
  10. short sig_exc;        /* used in intr.s */
  11.  
  12. /*
  13.  * killgroup(pgrp, sig): send a signal to all members of a process group
  14.  * returns 0 on success, or an error code on failure
  15.  */
  16.  
  17. long
  18. killgroup(pgrp, sig)
  19.     int pgrp, sig;
  20. {
  21.     PROC *p;
  22.     int found = 0;
  23.  
  24.     TRACE(("killgroup %d %d", pgrp, sig));
  25.  
  26.     if (pgrp < 0)
  27.         return EINTRN;
  28.  
  29.     for (p = proclist; p; p = p->gl_next) {
  30.         if (p->pgrp == pgrp) {
  31.             post_sig(p, sig);
  32.             found++;
  33.         }
  34.     }
  35.     if (found) {
  36.         check_sigs();    /* see if the current process is affected */
  37.         return 0;
  38.     }
  39.     else {
  40.         DEBUG(("killgroup: no processes found"));
  41.         return EFILNF;
  42.     }
  43. }
  44.  
  45. /* post_sig: post a signal as being pending. It is assumed that the
  46.    caller has already verified that "sig" is a valid signal, and
  47.    moreover it is the caller's responsibility to call check_sigs()
  48.    if it's possible that p == curproc
  49.  */
  50.  
  51. void
  52. post_sig(p, sig)
  53.     PROC *p;
  54.     int sig;
  55. {
  56.     ulong sigm;
  57.  
  58. /* if process is ignoring this signal, do nothing
  59.  * also: signal 0 is SIGNULL, and should never be delivered through
  60.  * the normal channels (indeed, it's filtered out in dossig.c,
  61.  * but the extra sanity check here is harmless). The kernel uses
  62.  * signal 0 internally for some purposes, but it is handled
  63.  * specially (see supexec() in xbios.c, for example).
  64.  */
  65.     if (p->sighandle[sig] == SIG_IGN || sig == 0)
  66.         return;
  67.  
  68. /* if the process is already dead, do nothing */
  69.     if (p->wait_q == ZOMBIE_Q || p->wait_q == TSR_Q)
  70.         return;
  71.  
  72. /* mark the signal as pending */
  73.     sigm = (1L << (unsigned long)sig);
  74.     p->sigpending |= sigm;
  75.  
  76. /* if the signal is masked, do nothing further */
  77.     if ( (p->sigmask & sigm) != 0 )
  78.         return;
  79.  
  80. /* otherwise, make sure the process is awake */
  81.     if (p->wait_q && p->wait_q != READY_Q) {
  82.         rm_q(p->wait_q, p);
  83.         add_q(READY_Q, p);
  84.     }
  85. }
  86.  
  87. /*
  88.  * check_sigs: see if we have any signals pending. if so,
  89.  * handle them.
  90.  */
  91.  
  92. void
  93. check_sigs()
  94. {
  95.     ulong sigs, sigm;
  96.     int i;
  97.     short deliversig;
  98.  
  99.     if (curproc->pid == 0) return;
  100. top:
  101.     sigs = curproc->sigpending & ~(curproc->sigmask);
  102.     if (sigs) {
  103.         sigm = 2;
  104. /* with tracing we need a mechanism to allow a signal to be delivered
  105.  * to the child (curproc); Fcntl(...TRACEGO...) passes a SIGNULL to indicate that we
  106.  * should really deliver the signal, hence its always safe to remove it
  107.  * from pending.
  108.  */
  109.         deliversig = (curproc->sigpending & 1L);
  110.         curproc->sigpending &= ~1L;
  111.  
  112.         for (i = 1; i < NSIG; i++) {
  113.             if (sigs & sigm) {
  114.                 curproc->sigpending &= ~sigm;
  115.                 if (curproc->ptracer && !deliversig) {
  116.                     TRACE(("tracer being notified of signal %d", i));
  117.                     stop(i);
  118.         /* the parent may reset our pending signals, so check again */
  119.                     goto top;
  120.                 } else {
  121.                     ulong omask;
  122.  
  123.                     curproc->sigpending &= ~sigm;
  124.                     omask = curproc->sigmask;
  125.  
  126.         /* sigextra gives which extra signals should also be masked */
  127.                     curproc->sigmask |= curproc->sigextra[i] | sigm;
  128.                     handle_sig(i);
  129.  
  130.  
  131. /*
  132.  * POSIX.1-3.3.4.2(723) "If and when the user's signal handler returns
  133.  * normally, the original signal mask is restored."
  134.  *
  135.  * BUG?: This unmasking could unmask a pending signal which we will not
  136.  * see this time around (if the signal number is less than i) and which
  137.  * was not pending when we started; should we detect this condition and
  138.  * loop around for a second try? POSIX only guarantees delivery of
  139.  * one signal per kernel entry, so this shouldn't really be a problem.
  140.  */
  141.                     curproc->sigmask = omask;    /* unmask signals */
  142.                 }
  143.             }
  144.             sigm = sigm << 1;
  145.         }
  146.     }
  147. }
  148.  
  149. /*
  150.  * raise: cause a signal to be raised in the current process
  151.  */
  152.  
  153. void
  154. raise(sig)
  155.     int sig;
  156. {
  157.     post_sig(curproc, sig);
  158.     check_sigs();
  159. }
  160.  
  161. #ifdef EXCEPTION_SIGS
  162. /* exception numbers corresponding to signals */
  163. char excep_num[NSIG] =
  164. { 0, 0, 0, 0,
  165.   4,            /* SIGILL == illegal instruction */
  166.   9,            /* SIGTRAP == trace trap    */
  167.   4,            /* pretend SIGABRT is also illegal instruction */
  168.   8,            /* SIGPRIV == privileged instruction exception */
  169.   5,            /* SIGFPE == divide by zero */
  170.   0, 2,            /* SIGBUS == bus error */
  171.   3            /* SIGSEGV == address error */
  172. /* everything else gets zeros */
  173. };
  174.  
  175. /* a "0" means we don't print a message when it happens -- typically the
  176.    user is expecting a synchronous signal, so we don't need to report it
  177. */
  178.  
  179. const char *signames[NSIG] = { 0,
  180. 0, 0, 0, "ILLEGAL INSTRUCTION", "TRACE TRAP",
  181. 0, "PRIVILEGE VIOLATION", "DIVISION BY ZERO", 0, "BUS ERROR",
  182. "ADDRESS ERROR", "BAD SYSTEM CALL", 0, 0, 0,
  183. 0, 0, 0, 0, 0,
  184. 0, 0, 0, "CPU TIME EXHAUSTED", "FILE TOO BIG",
  185. 0, 0, 0, 0, 0
  186. };
  187.  
  188. /*
  189.  * replaces the TOS "show bombs" routine: for now, print the name of the
  190.  * interrupt on the console, and save info on the crash in the appropriate
  191.  * system area
  192.  */
  193.  
  194. void
  195. bombs(sig)
  196.     int sig;
  197. {
  198.     long *procinfo = (long *)0x380L;
  199.     int i;
  200.     CONTEXT *crash;
  201.  
  202.     if (signames[sig]) {
  203.         ALERT("%s: User PC=%lx (basepage=%lx)",
  204.             signames[sig],
  205.             curproc->ctxt[SYSCALL].pc, curproc->base);
  206.  
  207. /* save the processor state at crash time */
  208. /* assumes that "crash time" is the context curproc->ctxt[SYSCALL] */
  209. /* BUG: this is not true if the crash happened in the kernel; in the
  210.  * latter case, the crash context wasn't saved anywhere.
  211.  */
  212.         crash = &curproc->ctxt[SYSCALL];
  213.         *procinfo++ = 0x12345678L;    /* magic flag for valid info */
  214.         for (i = 0; i < 15; i++)
  215.             *procinfo++ = crash->regs[i];
  216.         *procinfo++ = crash->ssp;
  217.         *procinfo++ = ((long)excep_num[sig]) << 24L;
  218.         *procinfo = crash->usp;
  219.  
  220. /* we're also supposed to save some info from the supervisor stack. it's not
  221.  * clear what we should do for MiNT, since most of the stuff that used to be
  222.  * on the stack has been put in the CONTXT struct. Moreover, we don't want
  223.  * to crash because of an attempt to access illegal memory. Hence, we do
  224.  * nothing here...
  225.  */
  226.     }
  227. }
  228. #endif
  229.  
  230. /*
  231.  * handle_sig: do whatever is appropriate to handle a signal
  232.  */
  233.  
  234. static long unwound_stack = 0;
  235.  
  236. void
  237. handle_sig(sig)
  238.     int sig;
  239. {
  240.     long oldstack, newstack;
  241.     long *stack;
  242.     CONTEXT *call, oldsysctxt, newcurrent;
  243.     extern void sig_return();
  244.  
  245.     if (curproc->sighandle[sig] == SIG_IGN)
  246.         return;
  247.     else if (curproc->sighandle[sig] == SIG_DFL) {
  248. _default:
  249.         switch(sig) {
  250. #if 0
  251. /* Note: SIGNULL is filtered out in dossig.c and is never actually
  252.  * delivered (its only purpose for the user is to test for the existence of
  253.  * a process, it isn't a real signal). The kernel uses SIGNULL
  254.  * internally, but all such code does the signal handling "by hand"
  255.  * and so no default handling is necessary.
  256.  */
  257.         case SIGNULL:
  258. #endif
  259.         case SIGWINCH:
  260.         case SIGCHLD:
  261.             return;        /* do nothing */
  262.         case SIGSTOP:
  263.         case SIGTSTP:
  264.         case SIGTTIN:
  265.         case SIGTTOU:
  266.             stop(sig);
  267.             return;
  268.         case SIGCONT:
  269.             curproc->sigpending &= ~STOPSIGS;
  270.             return;
  271.  
  272. /* here are the fatal signals. for SIGINT, we use p_term(-32) so that
  273.  * TOS programs that catch ^C via the vector at 0x400 and which expect
  274.  * TOS's error code (-32) to be sent will work. For most other signals,
  275.  * we p_term with an error code; for SIGKILL, we don't want to allow
  276.  * the program any chance to recover, so we call terminate() directly
  277.  * to avoid calling through to the user's terminate vector.
  278.  */
  279.         case SIGINT:        /* ^C */
  280.             if (curproc->domain == DOM_TOS) {
  281.                 p_term(-32);
  282.                 return;
  283.             }
  284.             /* otherwise, fall through */
  285.         default:
  286. #ifdef EXCEPTION_SIGS
  287.             bombs(sig); /* tell the user what happened */
  288. #endif
  289.     /* the "sigmask" check is in case a bus error happens in the user's
  290.      * term_vec code; we don't want to get stuck in an infinite loop!
  291.      */
  292.             if ((curproc->sigmask & 1L) || sig == SIGKILL)
  293.                 terminate(sig << 8, ZOMBIE_Q);
  294.             else
  295.                 p_term(sig << 8);
  296.         }
  297.     }
  298.     else {        /* user wants to handle it himself */
  299.  
  300. /* another kludge: there is one case in which the p_sigreturn mechanism
  301.  * is invoked by the kernel, namely when the user calls Supexec()
  302.  * or when s/he installs a handler for the GEMDOS terminate vector (#0x102)
  303.  * and the program terminates. MiNT fakes the call to user code with
  304.  * signal 0 (SIGNULL); programs that longjmp out of the user function
  305.  * and are later sent back to it again (e.g. if ^C keeps getting pressed
  306.  * and a terminate vector has been installed) will grow the stack without
  307.  * bound unless we watch for this case.
  308.  *
  309.  * Solution (sort of): whenever Pterm() is called, we unwind the
  310.  * stack; otherwise, we let it grow, so that nested Supexec()
  311.  * calls work.
  312.  *
  313.  * Note that SIGNULL is thrown away when sent by user processes, 
  314.  * and the user can't mask it (it's UNMASKABLE), so there is
  315.  * is no possibility of confusion with anything the user does.
  316.  */
  317.         if (sig == 0) {
  318.     /* p_term() sets sigmask to let us know to do Psigreturn */
  319.             if (curproc->sigmask & 1L) {
  320.                 p_sigreturn();
  321.                 curproc->sigmask &= ~1L;
  322.             } else {
  323.                 unwound_stack = 0;
  324.             }
  325.         }
  326.  
  327.         call = &curproc->ctxt[SYSCALL];
  328. /*
  329.  * what we do is build two fake stack frames; the bottom one is
  330.  * for a call to the user function, with (long)parameter being the
  331.  * signal number; the top one is for sig_return.
  332.  * When the user function returns, it returns to sig_return, which
  333.  * calls into the kernel to restore the context in prev_ctxt
  334.  * (thus putting us back here). We can then continue on our way.
  335.  */
  336.  
  337. /* set a new system stack, with a bit of buffer space */
  338.         oldstack = curproc->sysstack;
  339.         newstack = ((long) ( (&newcurrent) - 3 )) - 12;
  340.  
  341.         if (newstack < (long)curproc->stack + ISTKSIZE + 256) {
  342.             ALERT("stack overflow");
  343.             goto _default;
  344.         }
  345.         else if ((long) curproc->stack + STKSIZE < newstack) {
  346.             FATAL("system stack not in proc structure");
  347.         }
  348.  
  349. /* unwound_stack is set by p_sigreturn() */
  350.         if (sig == 0 && unwound_stack)
  351.             curproc->sysstack = unwound_stack;
  352.         else
  353.             curproc->sysstack = newstack;
  354.         oldsysctxt = *call;
  355.         stack = (long *)(call->sr & 0x2000 ? call->ssp :
  356.                 call->usp);
  357. /*
  358.    Hmmm... here's another potential problem for the signal 0 terminate
  359.    vector: if the program keeps returning back to user mode without
  360.    worrying about the supervisor stack, we'll eventually overflow it.
  361.    However, if the program is in supervisor mode itself, then we don't
  362.    want to stomp on its stack. Temporary solution: ignore the problem,
  363.    the stack's only growing 12 bytes at a time.
  364.  */
  365. /*
  366.  * in addition to the signal number we stuff the vector offset on the
  367.  * stack; if the user is interested they can sniff it, if not ignoring
  368.  * it needs no action on their part. Why do we need this? So that a
  369.  * single SIGFPE handler (for example) can discriminate amongst the
  370.  * multiple things which may get thrown its way
  371.  */
  372.         *(--stack) = (long)call->sfmt & 0xfff;
  373.         *(--stack) = (long)sig;
  374.         *(--stack) = (long)sig_return;
  375.         if (call->sr & 0x2000)
  376.             call->ssp = ((long) stack);
  377.         else
  378.             call->usp = ((long) stack);
  379.         call->pc = (long) curproc->sighandle[sig];
  380.         call->sfmt = call->fstate[0] = 0;    /* don't restart FPU communication */
  381.  
  382.         ((long *)curproc->sysstack)[1] = FRAME_MAGIC;
  383.         ((long *)curproc->sysstack)[2] = oldstack;
  384.         ((long *)curproc->sysstack)[3] = sig;
  385.  
  386.         if (curproc->sigflags[sig] & SA_RESET) {
  387.             curproc->sighandle[sig] = SIG_DFL;
  388.             curproc->sigflags[sig] &= ~SA_RESET;
  389.         }
  390.             
  391.         if (save_context(&newcurrent) == 0 ) {
  392. /*
  393.  * go do the signal; eventually, we'll restore this context (unless the
  394.  * user longjmp'd out of his signal handler). while the user is handling
  395.  * the signal, it's masked out to prevent race conditions. p_sigreturn()
  396.  * will unmask it for us when the user is finished.
  397.  */
  398.             newcurrent.regs[0] = CTXT_MAGIC;
  399.                 /* set D0 so next return is different */
  400.             assert(curproc->magic == CTXT_MAGIC);
  401.             leave_kernel();
  402.             restore_context(call);
  403.         }
  404. /*
  405.  * OK, we get here from p_sigreturn, via the user returning from
  406.  * the handler to sig_return. Restoring the stack and unmasking the
  407.  * signal have been done already for us by p_sigreturn.
  408.  * We should just restore the old system call context
  409.  * and continue with whatever it was we were doing.
  410.  */
  411.         TRACE(("done handling signal"));
  412.         curproc->ctxt[SYSCALL] = oldsysctxt;
  413.         assert(curproc->magic == CTXT_MAGIC);
  414.     }
  415. }
  416.  
  417. /*
  418.  * the p_sigreturn system call
  419.  * When called by the user from inside a signal handler, it indicates a
  420.  * desire to restore the old stack frame prior to a longjmp() out of
  421.  * the handler.
  422.  * When called from the sig_return module, it indicates that the user
  423.  * is finished a handler, and we should not only restore the stack
  424.  * frame but also the old context we were working in (which is on the
  425.  * system call stack -- see handle_sig).
  426.  * The "valid_return" variable is 0 in the first case, 1 in the second.
  427.  */
  428.  
  429. short valid_return;
  430.  
  431. long ARGS_ON_STACK
  432. p_sigreturn()
  433. {
  434.     CONTEXT *oldctxt;
  435.     long *frame;
  436.     long sig;
  437.  
  438.     unwound_stack = 0;
  439. top:
  440.     frame = (long *)curproc->sysstack;
  441.     frame++;    /* frame should point at FRAME_MAGIC, now */
  442.     sig = frame[2];
  443.     if (*frame != FRAME_MAGIC || (sig < 0) || (sig >= NSIG)) {
  444.         FATAL("Psigreturn: system stack corrupted");
  445.     }
  446.     if (frame[1] == 0) {
  447.         DEBUG(("Psigreturn: frame at %lx points to 0", frame-1));
  448.         return 0;
  449.     }
  450.     unwound_stack = curproc->sysstack;
  451.     TRACE(("Psigreturn(%d)", (int)sig));
  452.  
  453.     curproc->sysstack = frame[1];    /* restore frame */
  454.     curproc->sigmask &= ~(1L<<sig); /* unblock signal */
  455.  
  456.     if (!valid_return) {
  457. /* here, the user is telling us that a longjmp out of a signal handler is
  458.  * about to occur; so we should unwind *all* the signal frames
  459.  */
  460.         goto top;
  461.     }
  462.     else {
  463.         valid_return = 0;
  464.         oldctxt = ((CONTEXT *)(&frame[2])) + 3;
  465.         if (oldctxt->regs[0] != CTXT_MAGIC) {
  466.             FATAL("p_sigreturn: corrupted context");
  467.         }
  468.         assert(curproc->magic == CTXT_MAGIC);
  469.         restore_context(oldctxt);
  470.         return 0;    /* dummy -- this isn't reached */
  471.     }
  472. }
  473.  
  474. /*
  475.  * stop a process because of signal "sig"
  476.  */
  477.  
  478. void
  479. stop(sig)
  480.     int sig;
  481. {
  482.     unsigned int code;
  483.     unsigned long oldmask;
  484.     PROC *p;
  485.  
  486.     code = sig << 8;
  487.  
  488.     if (curproc->pid == 0) {
  489.         ALERT("attempt to stop MiNT");
  490.         return;
  491.     }
  492.  
  493. /* notify parent */
  494.     if (curproc->ptracer) {
  495.         p = curproc->ptracer;
  496.         post_sig(p, SIGCHLD);
  497.     } else {
  498.         p = pid2proc(curproc->ppid);
  499.         if (p && !(p->sigflags[SIGCHLD] & SA_NOCLDSTOP))
  500.             post_sig(p, SIGCHLD);
  501.     }
  502.  
  503.     oldmask = curproc->sigmask;
  504.  
  505.     if ((1L << sig) & STOPSIGS) {
  506.         /* mask out most signals */
  507.         curproc->sigmask |= ~(UNMASKABLE | SIGTERM);
  508.     }
  509.     else
  510.         assert(curproc->ptracer);
  511.  
  512. /* sleep until someone signals us awake */
  513.     sleep(STOP_Q, (long) code | 0177);
  514.  
  515. /* when we wake up, restore the signal mask */
  516.     curproc->sigmask = oldmask;
  517.  
  518. /* and discard any signals that would cause us to stop again */
  519.     curproc->sigpending &= ~STOPSIGS;
  520. }
  521.  
  522. /*
  523.  * interrupt handlers to raise SIGBUS, SIGSEGV, etc. Note that for
  524.  * really fatal errors we reset the handler to SIG_DFL, so that
  525.  * a second such error kills us
  526.  */
  527.  
  528. void
  529. exception(sig)
  530.     int sig;
  531. {
  532.     curproc->sigflags[sig] |= SA_RESET;
  533.     TRACE(("exception #%d raised", sig));
  534.     raise(sig);
  535. }
  536.  
  537. void
  538. sigbus()
  539. {
  540.     exception(SIGBUS);
  541. }
  542.  
  543. void
  544. sigaddr()
  545. {
  546.     exception(SIGSEGV);
  547. }
  548.  
  549. void
  550. sigill()
  551. {
  552.     exception(SIGILL);
  553. }
  554.  
  555. void
  556. sigpriv()
  557. {
  558.     raise(SIGPRIV);
  559. }
  560.  
  561. void
  562. sigfpe()
  563. {
  564.     extern short fpu;    /* in main.c */
  565.     
  566.     if (fpu) {
  567.         CONTEXT *ctxt;
  568.  
  569.         ctxt = &curproc->ctxt[SYSCALL];
  570.  
  571.     /* 0x1f38 is a Motorola magic cookie to detect a 68882 idle state frame */
  572.         if (*(ushort *)ctxt->fstate == 0x1f38 && 
  573.             (ctxt->sfmt & 0xfff) >= 0xc0L && (ctxt->sfmt & 0xfff) <= 0xd8L) {
  574.             /* fix a bug in the 68882 - Motorola call it a feature :-) */
  575.             ctxt->fstate[ctxt->fstate[1]] |= 1 << 3;
  576.         }
  577.     }
  578.     raise(SIGFPE);
  579. }
  580.  
  581. void
  582. sigtrap()
  583. {
  584.     raise(SIGTRAP);
  585. }
  586.  
  587. void
  588. haltformat()
  589. {
  590.     FATAL("halt: invalid stack frame format");
  591. }
  592.  
  593. void
  594. haltcpv()
  595. {
  596.     FATAL("halt: coprocessor protocol violation");
  597. }
  598.